<?php
/**
 * 产品分类
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Category;
use Common\Library\Page;
class CategoryController extends CommonController{
	private $msg=array();
	private $CateModel;
	function _initialize(){
		parent::_initialize();
		$this->CateModel=D('Category');
	}
	public function main(){
		$this->redirect('index');
	}
	public function index(){
		if(!$this->checkPermission('Category')) $this->jump(L('error_not_permission_page'),'error');
		$data=array();
		if(I('get.title')) { 
			$data['title']=I('get.title');
			$url['title']=I('get.title');
		}
		if(I('get.status')){
			$data['status']=I('get.status');
			$url['status']=I('get.status');
		} 
		$url['p']=I('get.p','');
		$list=$this->CateModel->getCategories($data);		
		$list=Category::unlimitedForLevel($list,'',0,0,'category_id','parent_id','title');
		$count=count($list);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$show=$Page->show();
		$list=array_splice($list,$Page->firstRow,$Page->listRows);
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();
	}
	public function oper(){
		$category_id=I('get.category_id',0);
		if(!$category_id && !$this->checkPermission('AddCategory')) $this->jump(L('error_not_permission_page'),'error');				
		$languages=get_langs();
		$category=$this->CateModel->getCategory($category_id);
		//如果传入了category_id却无法查询到记录，说明category_id是胡乱输入的
		if($category_id && empty($category)) $this->error(L('error_404'));		
		if(IS_POST){
			$category=I('post.');
			if($this->vilidateForm()){
				$this->CateModel->operCategory($category,$category_id);
				session('success',$category_id?L('success_edit'):L('success_add'));
				$url=array(
					'p'=>I('get.p',''),
					'title'=>I('get.title',''),
					'status'=>I('get.status','')
					);
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$category['thumb']=image_resize($category['image'],100,100);
		$category['placeholder']=image_resize();
		$this->assign('languages',$languages);
		$data_filter=array(
			'category_id'	=>	$category_id
			);		
		$categories=$this->CateModel->getCategories($data_filter);
		$categories=Category::unlimitedForLevel($categories,'',0,0,'category_id','parent_id', 'title');
		$this->assign('categories',$categories);
		$this->assign('msg',$this->msg);
		$this->assign('category',$category);
		$this->display();
	}

	public function del(){
		if($this->checkPermission('DeleteCategory')){
			if($this->vilidateDelete()){
				foreach (I('selected') as $category_id) {
					$count += $this->CateModel->del($category_id);
				}
				if($count){
					session('success',L('success_delete',array('count'=>$count)));
				}
			}else{
				session('danger',L('error_delete'));
			}
		}else{
			session('danger',L('error_not_permission'));
		}
		$url=array(
			'p'=>I('get.p',''),
			'title'=>I('get.title',''),
			'status'=>I('get.status','')
			);
		$this->redirect('index',$url);
	}
	/**
	 * 表单验证
	 */
	private function vilidateForm(){		
		$category_id=I('get.category_id',0,'trim');
		if($category_id && !$this->checkPermission('EditCategory')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach($data['category_description'] as $language_id => $value){
			if(mb_strlen($value['title'])<2 || mb_strlen($value['title'])>255){
				$this->msg['title'][$language_id]=L('error_title');
			}
			
			if(mb_strlen($value['meta_title'])>255){
				$this->msg['meta_title'][$language_id]=L('error_meta_title');
			}
			if(mb_strlen($value['meta_description'])>255){
				$this->msg['meta_description'][$language_id]=L('error_meta_description');
			}
			if(mb_strlen($value['meta_keyword'])>255){
				$this->msg['meta_keyword'][$language_id]=L('error_meta_keyword');
			}
			

		}
		return !$this->msg;
	}
	/**
	 * 删除前的操作，检查该分类下是否有内容，如果有则不予删除
	 */
	private function vilidateDelete(){
		$map['category_id']=array('in',I('post.selected'));
		$return=M('Product')->where($map)->select();
		return empty($return);
	}
	/**
	 * 自动完成
	 */
	public function autocomplete(){
		$json=array();
		$title=I('get.category_title','','htmlspecialchars,trim');
		$filter_data['status']=1;
		$categories=$this->CateModel->getCategories($filter_data);
		$categories=Category::unlimitedForLevel($categories,'',0,0,'category_id','parent_id','title');
		if($title){
			foreach ($categories as $category){
				$in_html=strripos($category['html'],$title);
				$in_title=strripos($category['title'],$title);
				if(($in_html !== false) || ($in_title !== false ) ){
					$json[]=array(
						'title'	=>	$category['title'],
						'category_id'			=>	$category['category_id'],
						'html'			=>	$category['html']
						);
				}
			}
		}else{
			$json=$categories;
		}
		$json=array_splice($json,0,8);
		$this->ajaxReturn($json,'JSON');
	}
}